package com.jy.novel.service;

import com.jy.novel.common.CommonResult;
import com.jy.novel.common.Page;
import com.jy.novel.dao.NovelDao;
import com.jy.novel.pojo.Novel;
import com.jy.novel.pojo.form.NovelAddForm;
import com.jy.novel.pojo.form.NovelEditForm;
import com.jy.novel.pojo.form.NovelQueryForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jihb on 4/14/16.
 */
@Service
public class NovelService {

    @Autowired
    NovelDao novelDao;

    /**
     * 分页查询小说 返回page
     * @param queryForm
     * @param curPage
     * @param pageSize
     * @return
     */
    public CommonResult<Page<Novel>> findNovelPage(NovelQueryForm queryForm, int curPage, int pageSize){
        return CommonResult.returnDataWrap(novelDao.findNovelPage(queryForm,curPage,pageSize));
    }

    /**
     * 分页查询小说 返回list
     * @param queryForm
     * @param curPage
     * @param pageSize
     * @return
     */
    public CommonResult<List<Novel>> findNovelList(NovelQueryForm queryForm, int curPage, int pageSize){
       return CommonResult.returnDataWrap(novelDao.findNovelList(queryForm,curPage,pageSize));
    }

    /**
     * 添加小说
     * @param addForm
     * @return
     */
    public CommonResult addNovel(NovelAddForm addForm){
        if (addForm == null){
            return CommonResult.returnFailure("缺少参数");
        }
        if (StringUtils.isBlank(addForm.getName())){
            return CommonResult.returnFailure("缺少小说名称");
        }
        if (addForm.getRootCategoryId() == null || addForm.getCategoryId()==null){
            return CommonResult.returnFailure("缺少小说所属分类");
        }
        if (addForm.getUserId()==null || addForm.getUserId() < 1){
            return CommonResult.returnFailure("缺少小说所属用户");
        }
        return CommonResult.returnBoolWrap(novelDao.addNovel(addForm),"添加小说失败");
    }

    /**
     * 修改小说
     * @param editForm
     * @param id
     * @return
     */
    public CommonResult updateNovel(NovelEditForm editForm, long id){
        if (editForm == null){
            return CommonResult.returnFailure("缺少参数");
        }
        if (id < 1){
            return CommonResult.returnFailure("小说id错误");
        }
        return CommonResult.returnBoolWrap(novelDao.updateNovel(editForm,id),"修改小说失败");
    }

    /**
     * 删除小说
     * @param id
     * @return
     */
    public CommonResult deleteNovel(long id){
        if (id< 1){
            return CommonResult.returnFailure("小说id错误");
        }
        return CommonResult.returnBoolWrap(novelDao.deleteNovel(id),"删除小说失败");
    }
}
